package com.qcby.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qcby.community.annotation.LogAnnotation;
import com.qcby.community.entity.Menu;
import com.qcby.community.entity.RoleMenu;
import com.qcby.community.entity.UserRole;
import com.qcby.community.form.MenuForm;
import com.qcby.community.mapper.MenuMapper;
import com.qcby.community.service.MenuService;
import com.qcby.community.service.RoleMenuService;
import com.qcby.community.util.Result;
import com.qcby.community.vo.MenuRoleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单管理 前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-03-25
 */
@RestController
@RequestMapping("/sys/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleMenuService roleMenuService;

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        List<MenuRoleVO> menuRoleList = this.menuService.getMenuRoleList();
        return Result.ok().put("data",menuRoleList);
    }

    /**
     * 添加
     * @param menu
     * @return
     */
    @LogAnnotation("添加菜单")
    @PostMapping("/add")
    public Result add(@RequestBody Menu menu){
        boolean save = this.menuService.save(menu);
        if(!save){
            return Result.error("添加菜单失败");
        }
        return Result.ok();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Integer id){
        Menu menu = this.menuService.getById(id);
        return Result.ok().put("data", menu);
    }

    /**
     * 修改
     * @param menuForm
     * @return
     */
    @LogAnnotation("编辑菜单")
    @PutMapping("/edit")
    public Result edit(@RequestBody MenuForm menuForm){
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuForm,menu);
        menu.setMenuId(menuForm.getId());
        boolean updateById = this.menuService.updateById(menu);
        if(!updateById){
            return Result.error("编辑菜单失败");
        }
        return Result.ok();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @LogAnnotation("删除菜单")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Integer id){
        boolean remove = this.menuService.removeById(id);
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
        menuQueryWrapper.eq("parent_id",id);
        this.menuService.remove(menuQueryWrapper);
        QueryWrapper<RoleMenu> roleMenuQueryWrapper = new QueryWrapper<>();
        roleMenuQueryWrapper.eq("menu_id",id);
        this.roleMenuService.remove(roleMenuQueryWrapper);
        //通过父id查询数据库返回的所有子id进行删除
        if(remove){
            return Result.ok();
        }
        return Result.error("删除菜单失败");
    }


}

